package com.jlzj.scanner;

import com.jlzj.test.TestScanner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO:
 *
 * @author jiangyangchuan
 * @date 2022/8/14 0014 16:32
 */

public class MyClassScanner {

    public static List<Class> doScan(String path) throws ClassNotFoundException {
        List<Class> classList = new ArrayList<>();
        String formatPath = parsePath(path);
        File file = new File(formatPath);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (null != listFiles) {
                for (File listFile : listFiles) {
                    String classPath = listFile.getPath();
                    String classFileName = classPath.substring(classPath.lastIndexOf("\\")+1);
                    String fileName = path+"."+classFileName.replace(".class","");
                    Class clazz = MyClassScanner.class.getClassLoader().loadClass(fileName);
                    classList.add(clazz);
                }
            }
        }
        return classList;
    }

    static String parsePath(String path) {
        String basePath = TestScanner.class.getClass().getResource("/").getPath();
        String tempPath = path.replace(".", "/");
        return basePath + tempPath;
    }
}
